import 'package:flutter/material.dart';

import 'base_problem.dart';
import 'best_chess.dart';
import 'chess_stage.dart';
import 'util/tool.dart';

/// 棋步评价颜色枚举
enum ChessColor {
  golden('#FFD400', '神韵'),
  supreme('#1772C5', '至臻'),
  proper('#2E8B57', '合宜'),
  acceptable('#CCCCFF', '尚可'),
  inappropriate('#F25A47', '不妥'),
  incorrect('#DC3023', '不当'),
  blunder('#8A2A21', '大错');

  final String hex;
  final String remark;

  const ChessColor(this.hex, this.remark);
}

/// 当前棋步的好坏判定
/// [isBlack] 是否黑棋
/// [wrl] 胜率损失
/// [currWinRate] 当前胜率
/// [tempWsl] 临时目损
/// [choose] 排序
/// [p] 基础问题概率
/// [stage] 阶段(1-5)
/// [i] 备用参数
ChessColor chessColorFlag(bool isBlack, double wrl, double currWinRate,
    double tempWsl, int choose, double p, ChessStage stage, int i) {
  bool isBest = isBestChess(isBlack, tempWsl, stage.index + 1, choose);
  if (isBest) return ChessColor.golden;
  if (choose == 1) return ChessColor.supreme;
  if (isBaseFunctionProblem(p)) return ChessColor.blunder;

  /// 是否有跃迁
  bool isJump = false;
  double preWinRate = currWinRate - wrl;
  if (preWinRate > 0.5 && currWinRate < 0.5) {
    isJump = true;
  }

  var wsl = isBlack ? tempWsl : -tempWsl;

  bool isChooseInRange(int lower, int upper) {
    return choose >= lower && choose <= upper;
  }

  ChessColor _color1() {
    return isChooseInRange(2, 3) ? ChessColor.supreme : ChessColor.proper;
  }

  ChessColor _color2() {
    return isChooseInRange(2, 6) ? ChessColor.proper : ChessColor.acceptable;
  }

  ChessColor _color3() {
    return isChooseInRange(2, 6)
        ? ChessColor.acceptable
        : ChessColor.inappropriate;
  }

  ChessColor _color4() {
    return isChooseInRange(2, 6)
        ? ChessColor.inappropriate
        : ChessColor.incorrect;
  }

  ChessColor _color5() {
    return ChessColor.blunder;
  }

  final colors = [_color1(), _color2(), _color3(), _color4(), _color5()];

  ChessColor _getColor(int startIndex, ChessStage stage, double wsl) {
    final thresholds = {
      1: [-0.16, -0.32, -0.48, -1.2],
      2: [-0.3, -0.6, -0.9, -2.25],
      3: [-0.44, -0.88, -1.32, -6.6],
      4: [-0.45, -0.9, -1.35, -6.75],
      5: [-0.4, -0.8, -1.2, -6]
    };

    final stageThresholds = thresholds[stage.index + 1] ?? thresholds[3]!;

    for (int i = startIndex; i < stageThresholds.length; i++) {
      if (wsl > stageThresholds[i]) {
        return colors[i];
      }
    }

    return colors.last;
  }

  if (isJump) {
    if (wrl < 0.25) {
      if ((stage == ChessStage.opening && wsl > -0.48) ||
          (stage == ChessStage.early_middle_game && wsl > -0.9) ||
          (stage == ChessStage.middle_game && wsl > -1.32) ||
          (stage == ChessStage.late_middle_game && wsl > -1.35) ||
          (stage == ChessStage.endgame && wsl > -1.2)) {
        return _color3();
      }
    } else if (wrl < 0.4) {
      if ((stage == ChessStage.opening && wsl > -1.2) ||
          (stage == ChessStage.early_middle_game && wsl > -2.25) ||
          (stage == ChessStage.middle_game && wsl > -6.6) ||
          (stage == ChessStage.late_middle_game && wsl > -6.75) ||
          (stage == ChessStage.endgame && wsl > -6)) {
        return _color2();
      }
    }
  } else {
    if (wrl < 0.1) {
      return _getColor(0, stage, wsl);
    } else if (wrl < 0.15) {
      return _getColor(1, stage, wsl);
    } else if (wrl < 0.25) {
      return _getColor(2, stage, wsl);
    } else if (wrl < 0.4) {
      return _getColor(3, stage, wsl);
    } else {
      return _color5();
    }
  }
  return _color5();
}

/// 获取颜色对应的评价文本
String remarkByColor(ChessColor color) {
  return color.remark;
}

/// 不属于问题手
bool isNotProblem(Color color) {
  List list = [
    getColorFromHex(ChessColor.golden.hex),
    getColorFromHex(ChessColor.supreme.hex),
    getColorFromHex(ChessColor.proper.hex),
    getColorFromHex(ChessColor.acceptable.hex)
  ];

  return list.contains(color);
}
